﻿using System.Collections;
using System.Collections.Generic;

namespace com.susi.util {
    public static class IListAndICollectionExtensions {
        /// <summary>
        /// 如果list为空则会创建
        /// <para>没有无参构造的类型，应自己封装属性</para>
        /// </summary>
        [System.Obsolete]
        public static void NewAdd<TCollection, TItem>(this TCollection list, TItem item)
            where TCollection : ICollection<TItem>, new() {
            if (list == null) list = new TCollection();
            list.Add(item);
        }

        /// <summary>
        /// 如果list为空则会创建，创建后需要赋值回去，会多次调用Add
        /// <para>没有无参构造的类型，应自己封装属性</para>
        /// </summary>
        [System.Obsolete]
        public static TList NewAdd<TList, TItem>(this TList list, IEnumerable<TItem> item)
            where TList : ICollection<TItem>, new()
        {
            if (list == null) list = new TList();
            foreach (var i in item)
            {
                list.Add(i);
            }
            return list;
        }



        /// <summary>
        /// 如果list为空则会创建，会调用<see cref="List{T}.AddRange(IEnumerable{T})"/>
        /// <para>没有无参构造的类型，应自己封装属性</para>
        /// </summary>
        [System.Obsolete]
        public static void NewAdd<TItem>(this List<TItem> list, IEnumerable<TItem> item) {
            if (list == null) list = new List<TItem>();
            list.AddRange(item);
        }
    }
}